package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ManipulateGridWCL extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ManipulateGridWCL() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doPost(request,response);
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/text");
		response.setCharacterEncoding( wid.UIcodeSet );
		
		Connection conn = wid.getConn(true);
		PreparedStatement ps = null;
		ResultSet rs = null;
		String errPars = "}";
		String strSQL = "";
		String oper = request.getParameter("oper");											//增刪改查那一種
		final String bssId =  request.getParameter("bssId");										//系統ID
		String subSysId = request.getParameter("subSysId");							//子系統ID
		int webconsolesn = 0;
		String webconsoleip = request.getParameter("webconsoleip");		//Web Console主機IP
		String modulePath = request.getParameter("modulePath");				//模組置放路徑
		String result = "";
		final String currentUser = request.getParameter("currentUser");
		String loginId = request.getParameter("loginId");									//登入帳號
		String loginPWD = request.getParameter("pwd");						//登入密碼
		
		try {
			
			/** 判別需進行何種操作 **/
			if ( "del".equalsIgnoreCase(oper) ) {
				webconsolesn = Integer.parseInt( request.getParameter("id") );
				
				strSQL = "DELETE FROM wcl WHERE webconsolesn=?";
				errPars =
						wid.toJend("webconsolesn", webconsolesn);
				ps = conn.prepareStatement(strSQL);
				ps.setInt(1, webconsolesn);
				ps.execute();
				
				wid.keepLogProfile(wid.WCL, wid.DELETE, currentUser, conn, ps);
				result = "刪除了"+webconsolesn;
			} else if ( "add".equalsIgnoreCase(oper) ) {
				webconsolesn = wid.getCurrentSN("wcl", conn, ps, rs);
				
				strSQL = "INSERT INTO wcl (webconsolesn,webconsoleip,modulepath,loginid,pwd,bssid,subsysid,creator,createdDateTime) VALUES (?,?,?,?,?,?,?,?,?)";
				errPars =
						wid.toJ("webconsolesn", webconsolesn)+
						wid.toJ("webconsoleip", webconsoleip)+
						wid.toJ("modulepath", modulePath)+
						wid.toJ("loginid", loginId)+
						wid.toJ("pwd", webconsolesn)+
						wid.toJ("bssid", loginPWD)+
						wid.toJend("subsysid", webconsolesn);
						
				ps = conn.prepareStatement(strSQL);
				ps.setInt(1, webconsolesn);
				ps.setString(2, webconsoleip);
				ps.setString(3, modulePath);
				ps.setString(4, loginId);
				ps.setString(5, loginPWD);
				ps.setString(6, bssId);
				ps.setString(7, subSysId );
				ps.setString(8, currentUser);
				ps.setTimestamp(9, wid.currentTimeStamp());
				ps.execute();
				
				wid.keepLogProfile(wid.WCL, wid.ADD, currentUser, conn, ps);
				result = "新增完成 "+webconsolesn;
			} else if ( "edit".equalsIgnoreCase(oper) ) {
				webconsolesn = Integer.parseInt( request.getParameter("id") );
				
				strSQL = "UPDATE wcl SET webconsoleip=?,modulepath=?,loginid=?,pwd=?,bssid=?,subsysid=?,lastUpdator=?,lastUpdatedDateTime=? WHERE webconsolesn=?";
				errPars =
						wid.toJ("webconsoleip", webconsoleip) +
						wid.toJ("modulepath", modulePath) +
						wid.toJ("loginid", loginId) +
						wid.toJ("pwd", loginPWD) +
						wid.toJ("bssid", bssId) +
						wid.toJend("subsysid", subSysId);
				
				ps = conn.prepareStatement(strSQL);
				ps.setString(1, webconsoleip);
				ps.setString(2, modulePath);
				ps.setString(3, loginId);
				ps.setString(4, loginPWD);
				ps.setString(5, bssId);
				ps.setString(6, subSysId);
				ps.setString(7, currentUser);
				ps.setTimestamp(8, wid.currentTimeStamp());
				ps.setInt(9, webconsolesn);
				ps.execute();
				
				wid.keepLogProfile(wid.WCL, wid.UPDATE, currentUser, conn, ps);
				result = "修改完成!! "+"\n"+"系統ID："+bssId+"\n"+"子系統ID："+subSysId+"\n"+"Web Console："+webconsolesn+"\n";
				
			}
		} catch (SQLException e) {
			result = "異動"+webconsolesn+"失敗!!\n\n";
			result += wid.errProcess(e);
			wid.kem(conn, ps, e, wid.toEType("SQLException")+errPars, wid.isPrint2Console );
		} catch (Exception e) {
			result = "異動"+webconsolesn+"失敗!!\n\n";
			result += wid.errProcess(e);
			wid.kem(conn, ps, e, wid.toEType("Exception")+errPars, wid.isPrint2Console );
		} finally {
			try {
				if (rs!= null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				System.out.println(wid.errProcess(e));
			}			
		}
		
		response.getWriter().write(result);		
	}
}